//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	DictionaryList.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace Smart
{
	/// <summary>
	/// Dictionary to items List
	/// </summary>
	[Serializable]
	[DebuggerStepThrough]
	public class DictionaryList<TKeyType, TItemType> : Dictionary<TKeyType, List<TItemType>>
	{
		public virtual bool SafeExists(TKeyType objectId)
		{
			List<TItemType> tmp;

			return TryGetValue(objectId, out tmp) && tmp.Count > 0;
		}

		public virtual int SafeGetCount(TKeyType objectId)
		{
			List<TItemType> tmp;

			return TryGetValue(objectId, out tmp) ? tmp.Count : 0;
		}

		public virtual TItemType SafeGet(TKeyType objectId, int idx)
		{
			List<TItemType> tmp;

			return TryGetValue(objectId, out tmp) && idx >= 0 && idx < tmp.Count ? tmp[idx] : default(TItemType);
		}

		public virtual List<TItemType> SafeGet(TKeyType objectId)
		{
			List<TItemType> tmp;

			TryGetValue(objectId, out tmp);

			return tmp;
		}

		public virtual void SafeSet(TKeyType objectId, int idx, TItemType item)
		{
			List<TItemType> tmp;

			if (TryGetValue(objectId, out tmp))
				tmp[idx] = item;
		}

		public virtual int SafeAdd(TKeyType objectId, TItemType item)
		{
			List<TItemType> tmp;

			if (!TryGetValue(objectId, out tmp))
				this[objectId] = tmp = new List<TItemType>();

			tmp.Add(item);

			return tmp.Count - 1;
		}

		public virtual void SafeRemove(TKeyType objectId, int idx)
		{
			List<TItemType> tmp;

			if (!TryGetValue(objectId, out tmp))
				return;

			tmp.RemoveAt(idx);

			if (tmp.Count <= 0)
				Remove(objectId);
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList()
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(int capacity)
			: base(capacity)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(IEqualityComparer<TKeyType> comparer)
			: base(comparer)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(int capacity, IEqualityComparer<TKeyType> comparer)
			: base(capacity, comparer)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		protected DictionaryList(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}
	}

	/// <summary>
	/// Dictionary to items List
	/// </summary>
	[Serializable]
	[DebuggerStepThrough]
	public class DictionaryList<TKeyType, TValueType, TItemType> : Dictionary<TKeyType, Cortege<TValueType, List<TItemType>>>
	{
		public virtual bool SafeExists(TKeyType objectId)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			return TryGetValue(objectId, out tmp) && tmp.Arg2.Count > 0;
		}

		public virtual int SafeGetCount(TKeyType objectId)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			return TryGetValue(objectId, out tmp) ? tmp.Arg2.Count : 0;
		}

		public virtual TItemType SafeGet(TKeyType objectId, int idx)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			return TryGetValue(objectId, out tmp) && idx >= 0 && idx < tmp.Arg2.Count ? tmp.Arg2[idx] : default(TItemType);
		}

		public virtual Cortege<TValueType, List<TItemType>> SafeGet(TKeyType objectId)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			TryGetValue(objectId, out tmp);

			return tmp;
		}

		public virtual TValueType SafeGetValue(TKeyType objectId)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			return TryGetValue(objectId, out tmp) ? tmp.Arg1 : default(TValueType);
		}

		public virtual List<TItemType> SafeGetList(TKeyType objectId)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			return TryGetValue(objectId, out tmp) ? tmp.Arg2 : null;
		}

		public virtual void SafeSet(TKeyType objectId, int idx, TItemType item)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			if (TryGetValue(objectId, out tmp))
				tmp.Arg2[idx] = item;
		}

		public virtual int SafeAdd(TKeyType objectId, TValueType value, TItemType item)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			if (!TryGetValue(objectId, out tmp))
				this[objectId] = tmp = new Cortege<TValueType, List<TItemType>>(false, value, new List<TItemType>());

			var list = tmp.Arg2;

			list.Add(item);

			return list.Count - 1;
		}

		public virtual void SafeRemove(TKeyType objectId, int idx)
		{
			Cortege<TValueType, List<TItemType>> tmp;

			if (!TryGetValue(objectId, out tmp))
				return;

			var list = tmp.Arg2;

			list.RemoveAt(idx);

			if (list.Count <= 0)
				Remove(objectId);
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList()
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(int capacity)
			: base(capacity)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(IEqualityComparer<TKeyType> comparer)
			: base(comparer)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		public DictionaryList(int capacity, IEqualityComparer<TKeyType> comparer)
			: base(capacity, comparer)
		{
		}

		/// <summary>
		/// Dictionary to items List
		/// </summary>
		protected DictionaryList(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}
	}
}